package com.hornsun.repository;

import com.hornsun.domain.dbo.Favourite;
import com.hornsun.domain.dbo.FavouriteExample;
import com.hornsun.domain.dbo.Information;
import com.hornsun.repository.mapper.FavouriteMapper;
import com.hornsun.repository.mapper.InformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * Created by 编译中... on 2018/3/31.
 */
@Repository
public class FavouriteRepository {

    @Resource
    private FavouriteMapper favouriteMapper;

    @Resource
    private InformationMapper informationMapper;

    public void insertFavourite(Favourite favourite){
        int count = favouriteMapper.insert(favourite);
        if (count!=1){
            throw new IllegalArgumentException("insert favourite failed by favourite:"+favourite);
        }
    }


    /**
     * 取消收藏
     * @param openid
     * @param guid
     */
    public void deleteFavourite(String openid,String guid){
        FavouriteExample favouriteExample = new FavouriteExample();
        FavouriteExample.Criteria criteria = favouriteExample.createCriteria();
        criteria.andCollectContentGuidEqualTo(guid).andOpenidEqualTo(openid);
        int count = favouriteMapper.deleteByExample(favouriteExample);
        if (count!=1){
            throw new IllegalArgumentException("delete favourite failed by guid:"+guid);
        }
    }

    //查看当前用户是否收藏
    public boolean checkFavourite(String openid ,String guid){
        FavouriteExample favouriteExample = new FavouriteExample();
        FavouriteExample.Criteria criteria = favouriteExample.createCriteria();
        criteria.andCollectContentGuidEqualTo(guid).andOpenidEqualTo(openid);
        return favouriteMapper.countByExample(favouriteExample) == 1;
    }


   
}
